package com.xinchengxd.chat;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinchengxd.commons.ResponseResult;

import javax.websocket.DecodeException;
import javax.websocket.Decoder;
import javax.websocket.EndpointConfig;

/**
 * @author: 姚新成
 * @Date: 2021/11/17:17:10
 * @Description: 统一响应的解码器
 */

public class MyDecoder implements Decoder.Text<ResponseResult> {

    @Override
    public ResponseResult<String> decode(String s) throws DecodeException {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(s, ResponseResult.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean willDecode(String s) {
        return true;
    }

    @Override
    public void init(EndpointConfig endpointConfig) {

    }

    @Override
    public void destroy() {

    }
}
